//
//  YAYISettingCell.m
//  YAYIDoctor
//
//  Created by Veer on 16/5/6.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "XMeCell.h"

#import "XSettingItemModel.h"

@interface XMeCell()

@property (nonatomic, weak) UIView *bottomLineView;
@property (nonatomic, weak) UIImageView *accessoryImageView;

@property (nonatomic, weak) UILabel *latestFunctionLbl;
@property (nonatomic, weak) UIImageView *latestFunctionImgView;

@property (nonatomic, weak) UISwitch *sw;


@end


@implementation XMeCell


- (void)setModel:(XSettingItemModel *)model
{
    _model = model;
    
    //
    if (model.icon) {
        self.imageView.image = [UIImage imageNamed:model.icon];
    }
    
    self.textLabel.text = model.name;
    
    if (model.detail.length > 0) {
        self.detailLbl.text = model.detail;
        self.detailLbl.textColor = RGB(153, 153, 153);
    }else if(model.signalStr){
        self.detailLbl.text = model.detail;
        self.detailLbl.textColor = RGB(252, 128, 57);
    }
    
//    self.pointView.hidden = !model.isOn;
    self.sw.hidden = !model.isHaveSwitch;
    self.accessoryImageView.hidden = model.isHaveSwitch;
    if (model.isHaveSwitch) {
        self.sw.on = model.isOn;
    }
    
    self.latestFunctionLbl.hidden = !model.signalStr;
    self.latestFunctionImgView.hidden = !model.signalStr;
}


- (void)setLineType:(LineType)lineType
{
    _lineType = lineType;
    if (lineType == LineTypeCenter) {
        
        self.bottomLineView.hidden = NO;
    }else{
        
        self.bottomLineView.hidden = YES;
    }
}


+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"XMeCell";
    
    XMeCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        
        cell = [[XMeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.accessoryType = UITableViewCellAccessoryNone;
        // lieview
        [self setupSubView];
        
    }
    return self;
}


- (void)setupSubView
{
    
    self.textLabel.font = VRFont(16);
    self.textLabel.textColor = RGB(51, 51, 51);
    
    UIView *selectedBgView = [[UIView alloc] init];
    selectedBgView.backgroundColor = RGB(238, 238, 238);
    self.selectedBackgroundView = selectedBgView;
    
    UILabel *detailLbl = [[UILabel alloc] init];
    detailLbl.textColor = RGB(153, 153, 153);
    detailLbl.font = VRFont(14);
    detailLbl.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:detailLbl];
    self.detailLbl = detailLbl;
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = RGB(228, 228, 228);
    [self.contentView addSubview:bottomLineView];
    self.bottomLineView = bottomLineView;
    
    UIImageView *accessoryImageView = [[UIImageView alloc] init];
    accessoryImageView.contentMode = UIViewContentModeScaleAspectFit;
    accessoryImageView.image = [UIImage imageNamed:@"arrow_right"];
    [self.contentView addSubview:accessoryImageView];
    self.accessoryImageView = accessoryImageView;
    
    // 红色点，换肤用
    UIView *pointView = [[UIView alloc] init];
    pointView.layer.cornerRadius = 5;
    pointView.hidden = YES;
//    pointView.tintAdjustmentMode = UIViewTintAdjustmentMode;
    [self.contentView addSubview:pointView];
    self.pointView = pointView;
    
    UISwitch *sw = [[UISwitch alloc] init];
    sw.hidden = YES;
    sw.onTintColor = MAINColor;
    [self.contentView addSubview:sw];
    self.sw = sw;
    [sw addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventValueChanged];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.detailLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.accessoryImageView.mas_left).offset(-12);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.accessoryImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [self.pointView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.detailLbl.mas_left).offset(-5);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(10);
    }];
    
    [self.sw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(51);
        make.height.mas_equalTo(30);
    }];
}


#pragma mark - action
- (void)switchClick:(UISwitch *)sw
{

    if (self.switchChangeOperation) {
        self.switchChangeOperation(sw.isOn);
    }
}


- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
//    self.pointView.sakura.backgroundColor(@"Navigation.backgroundColor");
}


- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
}


@end
